package com.puboot.module.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.puboot.common.util.CoreConst;
import com.puboot.common.util.ResultUtil;
import com.puboot.module.admin.model.BizCourse;
import com.puboot.module.admin.service.BizCourseService;
import com.puboot.module.admin.vo.base.PageResultVo;
import com.puboot.module.admin.vo.base.ResponseVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 课程管理S
 * @author ltc
 * @version 1.0
 * @date 2020/9/28 21:43
 */
@Controller
@RequestMapping("course")
@AllArgsConstructor
public class BizCourseController {

    private final BizCourseService bizCourseService;

    @GetMapping("/add")
    public String add() {
        return CoreConst.ADMIN_PREFIX + "course/form";
    }




    @PostMapping("list")
    @ResponseBody
    public PageResultVo loadcourses(BizCourse bizCourse, Integer pageNumber, Integer pageSize) {
        IPage<BizCourse> bizCourseIPage = bizCourseService.PageCourses(bizCourse, pageNumber, pageSize);
        return ResultUtil.table(bizCourseIPage.getRecords(), bizCourseIPage.getTotal());
    }

    @PostMapping("/add")
    @ResponseBody
    public ResponseVo add(BizCourse bizCourse) {
        bizCourse.setStatus(1);
        Date date = new Date();
        bizCourse.setCreateTime(date);
        bizCourse.setUpdateTime(date);
        boolean i = bizCourseService.save(bizCourse);
        if (i)
            return ResultUtil.success("新增课程成功");
        return ResultUtil.error("新增课程失败");
    }

    @GetMapping("/edit")
    public String edit(Model model, Integer id) {
        BizCourse bizCourse = bizCourseService.getById(id);
        model.addAttribute("course", bizCourse);
        return CoreConst.ADMIN_PREFIX + "course/form";
    }

    @PostMapping("/edit")
    @ResponseBody
    public ResponseVo edit(BizCourse bizCourse) {
        bizCourse.setUpdateTime(new Date());
        boolean i = bizCourseService.updateById(bizCourse);
        if (i)
            return ResultUtil.success("编辑课程成功");
        return ResultUtil.error("编辑课程失败");

    }

    @PostMapping("/delete")
    @ResponseBody
    public ResponseVo delete(Integer id) {
        boolean i = bizCourseService.removeById(id);
        if (i)
            return ResultUtil.success("删除课程成功");
        return ResultUtil.error("删除课程失败");

    }

    @PostMapping("/batch/delete")
    @ResponseBody
    public ResponseVo deleteBatch(@RequestParam("ids[]") Integer[] ids) {
        int i = bizCourseService.deleteBatch(ids);
        if (i > 0)
            return ResultUtil.success("删除标签成功");
        return ResultUtil.error("删除标签失败");

    }
}
